use async_trait::async_trait;
use mf_derive::impl_command;
use mf_model::node_definition::NodeTree;
use mf_state::{Transaction, transaction::Command};
use mf_transform::TransformResult;
use std::sync::Arc;
#[derive(Clone, Default, Debug)]
pub struct MyCommand;
impl MyCommand {
    pub fn new() -> Arc<MyCommand> {
        Arc::new(MyCommand)
    }
}

#[async_trait]
impl Command for MyCommand {
    async fn execute(
        &self,
        tr: &mut Transaction,
    ) -> TransformResult<()> {
        //  数据库的查询
        let doc = tr.doc();
        let root = doc.root().unwrap();
        let node = tr.schema.factory().create_node("DW", None, None, vec![], None)?;
        tr.add_node(root.id.clone(), vec![NodeTree::from(node, vec![])])?;
        Ok(())
    }
    fn name(&self) -> String {
        "cassie".to_string()
    }
}
#[impl_command(MyCommond)]
pub async fn my_commond(tr: &mut Transaction) -> TransformResult<()> {
    let doc = tr.doc();
    let root = doc.root().unwrap();
    let node = tr.schema.factory().create_node("DW", None, None, vec![], None)?;
    tr.add_node(root.id.clone(), vec![NodeTree::from(node, vec![])])?;
    Ok(())
}
